package com.xca.is.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;
import com.xca.is.R;
import com.xca.is.adapter.TongYiAdapter;
import com.xca.is.entity.TongYi;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.TongYiModel;
import com.xca.is.utils.WebViewScriptUtils;

import java.util.ArrayList;
import java.util.List;

public class AiFragment extends Fragment implements HttpConnect {

    private RecyclerView recyclerView ;
    private TextInputEditText editText ;
    private ImageButton textView  ;
    private TextView textLoading ;

    private Spinner spinner1 , spinner2 ;
    private WebView webView ;

    private TongYiAdapter adapter ;
    private List<TongYi> list ;
    private TongYiModel model ;
    private ViewModelProvider provider ;
    private MutableLiveData<String> liveData ;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ai, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.tyRecycle);
        editText = view.findViewById(R.id.tyInput);
        textView = view.findViewById(R.id.tySend);
        textLoading = view.findViewById(R.id.tyLoading);
        spinner1 = view.findViewById(R.id.ai_spi1);
        spinner2 = view.findViewById(R.id.ai_spi2);
        webView = view.findViewById(R.id.ai_web);

        int[] ints = new int[]{10,18};
        WebViewScriptUtils.htmlContent(ints,webView);

        spinner2.setVisibility(View.GONE);
        spinnerSet();

        textLoading.setVisibility(View.GONE);
        list = new ArrayList<>();
        list.add(new TongYi("你好,我是智能回复助手",0));
        provider = new ViewModelProvider(this);
        model = provider.get(TongYiModel.class);
        model.setData(this);
        liveData = (MutableLiveData<String>) model.getLiveData();
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),1));
        liveData.observe(getActivity(), new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if (s != null){
                    list.add(new TongYi(s,0));
                    Log.d("从api获取到的数据", s);
                    adapter.updateList(list);
                    // Toast.makeText(TongYiActivity.this, String.valueOf(list.size()), Toast.LENGTH_SHORT).show();
                }
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = editText.getText().toString();

                list.add(new TongYi(text,1));
                editText.setText("");
                adapter.updateList(list);
                String type = spinner1.getSelectedItem().toString() ;
                String coder = spinner2.getSelectedItem().toString() ;
                // Toast.makeText(this, list.get(1).getText(), Toast.LENGTH_SHORT).show();
                model.getAi(type,text,coder);
            }
        });

        adapter = new TongYiAdapter(list,getContext(),getActivity());

        // 滚动到底部
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                recyclerView.scrollToPosition(adapter.getItemCount()-1);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void spinnerSet(){
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(getContext(),R.layout.ai_spinner_item,getResources().getStringArray(R.array.ai_spinner1));
        spinner1.setAdapter(adapter1);
        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_item);

        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(getContext(),R.layout.ai_spinner_item,getResources().getStringArray(R.array.ai_spinner2));
        spinner2.setAdapter(adapter2);
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);

        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String item = adapter1.getItem(i).toString();
                if (item.equals("Gpt人设")){
                    spinner2.setVisibility(View.VISIBLE);
                }else {
                    spinner2.setVisibility(View.GONE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String item = adapter2.getItem(i).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }


    @Override
    public void Loading(boolean loading) {
        textLoading.setVisibility(loading?View.VISIBLE:View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {
        if (msg!=null){
            list.add(new TongYi("抱歉，连接超时",0));
            adapter.updateList(list);
        }
    }

    @Override
    public void Complete() {
        textLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}